package com.example.wangxin.mytimeline;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RelativeLayout mRlFu;
    private ImageView mIvLine;
    private ImageView mIvTopLeft;
    private ImageView mIvTopRight;
    private ImageView mIvBottomLeft;
    private ImageView mIvBottomRight;
    private View mView;
    private int mWidth;
    private int mHeight;
    private RelativeLayout mRlOut;
    private TextView mTvTitle;
    private int mTopDistance;
    private DashLineView mIvDotLine;
    private int mLastDistance;
    private ImageView mIvShowLine;
    private ImageView mIvBetWeenLastLine;
    private RelativeLayout mRlContent;
    private int mContentHeight;
    //时间轴上标记的宽度
    private int mTimeMarkWidth;
    //时间轴上标记的高度
    private int mTimeMarkHeight;
    private int mShowLineWidth;
    private int mLastLineWidth;
    private List<AdBean> mDataList;
    private String mLastEndTime;
    private TextView mTvNum;
    private TextView mTvStartTime;
    private TextView mTvEndTime;
    private RelativeLayout mRlStartTime;
    private RelativeLayout mRlEndTime;
    //与上个条目之间的间隔距离
    private int toLastDistance = 50;
    //左边条目到左边框的距离
    private int mLefMargin = 10;
    //右边条目到时间轴的距离
    private int mRightToLine = 10;
    //条目对应的时间线的宽度
    private int mTimeLineWidth = 2;
    //与上个条目之间的时间线的宽度
    private int mLastBetWeenTimeLineWidth = 1;
    //时间高度
    private int mTimeHeight = 10;
    //时间宽度
    private int mTimeWidth = 10;
    //条目高度52dp
    private int mItemHeight = 82;
    //到顶部的距离
    private int toTopDistance = 40;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRlFu = (RelativeLayout) findViewById(R.id.rl_fu);
//        mIvLine = (ImageView) findViewById(R.id.iv_line);

        WindowManager manager = this.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        mWidth = outMetrics.widthPixels;
        mHeight = outMetrics.heightPixels;
        initData();

//        addMyView();
        AddViewUtil addViewUtil = new AddViewUtil(this, mWidth, toTopDistance, mItemHeight, mTimeWidth, mTimeHeight, mTimeLineWidth, mLastBetWeenTimeLineWidth, mRlFu, mDataList);
        addViewUtil.addView();
    }

    private void initData() {
        //创建测试数据
        mDataList = new ArrayList<>();
        mDataList.add(new AdBean("标题1", "包含2条广告", "1点", "2点"));
        mDataList.add(new AdBean("标题2", "包含2条广告", "3点", "5点"));
        mDataList.add(new AdBean("标题3", "包含3条广告", "5点", "6点"));
        mDataList.add(new AdBean("标题4", "包含2条广告", "7点", "8点"));
        mDataList.add(new AdBean("标题5", "包含7条广告", "9点", "10点"));
        mDataList.add(new AdBean("标题6", "包含6条广告", "10点", "11点"));
        mDataList.add(new AdBean("标题7", "包含1条广告", "12点", "13点"));
        mDataList.add(new AdBean("标题8", "包含7条广告", "14点", "15点"));
        mDataList.add(new AdBean("标题9", "包含3条广告", "15点", "16点"));
    }

    private void addMyView() {
//        mRlFu.removeAllViews();
        mTopDistance = dip2px(this, toTopDistance);
        //到上一个条目底部的距离
        mLastDistance = mTopDistance;
        mContentHeight = dip2px(this, mItemHeight);
        mTimeMarkWidth = dip2px(this, mTimeWidth);
        mTimeMarkHeight = dip2px(this, mTimeHeight);
        mShowLineWidth = dip2px(this, mTimeLineWidth);
        mLastLineWidth = dip2px(this, mLastBetWeenTimeLineWidth);
        //上个条目的结束时间
        mLastEndTime = "0点";
        for (int i = 0; i < mDataList.size(); i++) {

            mView = LayoutInflater.from(this).inflate(R.layout.fu, null);
            mRlOut = mView.findViewById(R.id.rl_out);
            mIvTopRight = mView.findViewById(R.id.iv_right);
            mIvBottomRight = mView.findViewById(R.id.iv_bottomright);
            mTvTitle = mView.findViewById(R.id.tv_title);
            mTvNum = mView.findViewById(R.id.tv_ad_num);
            mTvStartTime = mView.findViewById(R.id.tv_starttime);
            mTvEndTime = mView.findViewById(R.id.tv_endtime);
            mRlStartTime = mView.findViewById(R.id.rl_starttime);
            mRlEndTime = mView.findViewById(R.id.rl_endtime);

            mTvStartTime.setText(mDataList.get(i).getStartTime());
            mTvEndTime.setText(mDataList.get(i).getEndTime());

            mTvNum.setText(mDataList.get(i).getContent());
            mTvTitle.setText(mDataList.get(i).getTitle());
//            mIvDotLine = mView.findViewById(R.id.iv_dotline);

            //对应的时间线
            mIvShowLine = mView.findViewById(R.id.iv_show_line);
            //与上一个条目之间间隔的时间线
            mIvBetWeenLastLine = mView.findViewById(R.id.iv_betweenlast);
            //单个条目
            mRlContent = mView.findViewById(R.id.rl_content);
            if (i > 0) {
                if (!mDataList.get(i).getStartTime().equals(mLastEndTime)) {
                    mTopDistance = mTopDistance + toLastDistance;
                } else {
                    mRlStartTime.setVisibility(View.GONE);
                }
            }
            if (i % 2 == 0) {
                //设置条目位置
                contentPosition(0);
            } else {
                //设置条目位置
                contentPosition(1);
            }
            //条目对应的时间线的位置
            contentTimeLinePosition();

            //与上个条目之间的时间线的位置
            lastBetWeenTimeLinePosition();

            //开始点位置
            setStartTimePosition();

            //结束点位置
            mTopDistance = mTopDistance + mContentHeight - mTimeMarkHeight;
            setEndTimePosition();

            mLastDistance = mTopDistance;

            mLastEndTime = mDataList.get(i).getEndTime();
            mRlFu.addView(mView);
        }
    }

    /**
     * 设置当前条目的位置，i为0时位置在左边，1时在右边
     * @param i
     */
    private void contentPosition(int i) {
        RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if (i == 0) {
            layoutParams1.setMargins(dip2px(this, mLefMargin), mTopDistance, 0, 0);
        } else if (i == 1) {
            layoutParams1.setMargins(mWidth / 2 + dip2px(this, mRightToLine), mTopDistance, 0, 0);
        }
        mRlOut.setLayoutParams(layoutParams1);
    }

    /**
     * 设置当前条目与上个条目之间的时间时间线的位置
     */
    private void lastBetWeenTimeLinePosition() {
        ViewGroup.MarginLayoutParams ivBetWeenLastLinemargin = new ViewGroup.MarginLayoutParams(mIvBetWeenLastLine.getLayoutParams());
        ivBetWeenLastLinemargin.setMargins(mWidth / 2 - mShowLineWidth / 2, mLastDistance + mTimeMarkHeight, 0, 0);
        RelativeLayout.LayoutParams ivBetWeenLastLineLayoutParams = new RelativeLayout.LayoutParams(ivBetWeenLastLinemargin);
        ivBetWeenLastLineLayoutParams.width = mLastLineWidth;
        ivBetWeenLastLineLayoutParams.height = mTopDistance - mLastDistance - mTimeMarkHeight;
        mIvBetWeenLastLine.setLayoutParams(ivBetWeenLastLineLayoutParams);
    }

    /**
     * 设置条目对应的时间线的位置
     */
    private void contentTimeLinePosition() {
        ViewGroup.MarginLayoutParams ivShowLinemargin = new ViewGroup.MarginLayoutParams(mIvShowLine.getLayoutParams());
        ivShowLinemargin.setMargins(mWidth / 2 - mShowLineWidth / 2, mTopDistance + mTimeMarkHeight, 0, 0);
        RelativeLayout.LayoutParams ivShowLineparams = new RelativeLayout.LayoutParams(ivShowLinemargin);
        ivShowLineparams.width = mShowLineWidth;
        ivShowLineparams.height = mContentHeight - mTimeMarkHeight * 2;
        mIvShowLine.setLayoutParams(ivShowLineparams);
    }

    /**
     * 设置时间轴上结束点的位置
     */
    private void setEndTimePosition() {
        RelativeLayout.LayoutParams mRlEndParams = (RelativeLayout.LayoutParams) mRlEndTime.getLayoutParams();
        mRlEndParams.topMargin = mTopDistance;
        mRlEndParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        mRlEndTime.setLayoutParams(mRlEndParams);
    }

    /**
     * 设置时间轴上开始点的位置
     */
    private void setStartTimePosition() {
        RelativeLayout.LayoutParams mStartRlParams = (RelativeLayout.LayoutParams) mRlStartTime.getLayoutParams();
        mStartRlParams.topMargin = mTopDistance;
        mStartRlParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        mRlStartTime.setLayoutParams(mStartRlParams);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
